package com.newhero.product.module.sjzsw.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.Json;
import com.newhero.product.module.sjzsw.persistence.entity.WebOrgan;
import com.newhero.product.module.sjzsw.service.IWebOrganService;
import com.newhero.product.module.sys.model.Organ;
import com.newhero.product.module.sys.service.IOrganService;

 /**
 * @ClassName: WebOrganController
 * @Description: 访问控制类
 * @author liangzhuoxin
 * @date 2019-01-28
 */
@Controller
@RequestMapping("/docInfoService")
public class WebOrganController extends BaseController {
    
    @Autowired 
    private IWebOrganService webOrganServiceImpl;
    @Autowired 
    private IOrganService sysOrganServiceImpl;
    									
	@RequestMapping(value = "/getOrgans", method ={RequestMethod.GET,RequestMethod.POST })
	public void getOrgans(
			HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		
		{
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/plain; charset=utf-8");
			JSONObject jsonObject=new JSONObject();
			PrintWriter out = response.getWriter();
			String str=request.getParameter("str");
			if(str != null && !"".equals(str)){
				System.out.println("收到的信息："+str);
				try {
					JSONObject object=JSONObject.fromString(str);
					System.out.println("请求IP："+request.getRemoteAddr());
					String account = object.getString("account");
					String password = object.getString("password");
					int flag = 0;
					EntityWrapper<WebOrgan> organ = new EntityWrapper<WebOrgan>();
					organ.eq("account", account);
					organ.eq("passwd", password);
					flag = webOrganServiceImpl.selectCount(organ);
					
					if(flag > 0){
						EntityWrapper<Organ> sysOrgan = new EntityWrapper<Organ>();
						List<Organ> organList = sysOrganServiceImpl.selectList(sysOrgan);
						List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();
						for(Organ sysorgan : organList){
							Map<String,String> map = new HashMap<String,String>();
							map.put("code", sysorgan.getOrganID());
							map.put("name", sysorgan.getOrganName());
							mapList.add(map);
						}
						jsonObject.put("code", 1);
						jsonObject.put("data", mapList.toArray());
						jsonObject.put("msg", "成功");
					}else {
						jsonObject.put("code", -1);
						jsonObject.put("msg", "账号密码错误");
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					System.out.println(e);
					jsonObject.put("code", 0);
					jsonObject.put("msg", "失败");
				}
			}else {
				jsonObject.put("code", 0);
				jsonObject.put("msg", "参数为空");
			}
			System.out.println("返回信息："+jsonObject);
			out.print(jsonObject.toString());
			out.flush();
			out.close();
			
		}
	}
}
